package zzs.generalframework.console.core.parameter.commonparameter.address.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.address.AddressInfo;
import zzs.generalframework.common.core.parameter.commonparameter.address.AddressPO;
import zzs.generalframework.console.core.parameter.commonparameter.address.dao.IAddressDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.address.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.address.dao.impl.addressDaoImpl")
public class AddressDaoImpl implements IAddressDao
{
	private Logger logger = LoggerFactory.getLogger(AddressDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(AddressPO addressPO)
	{
		logger.info("====插入地址数据开始====");
		logger.debug("====插入地址数据,入参:AddressPO=[{}]====", addressPO);
		entityManager.persist(addressPO);
		logger.debug("====插入地址数据,出参:boolean=[{}]====", true);
		logger.info("====插入地址数据结束====");
		return true;
	}

	@Override
	public boolean update(AddressPO addressPO)
	{
		logger.info("====更新地址数据开始====");
		logger.debug("====更新地址数据,入参:AddressPO=[{}]====", addressPO);
		entityManager.merge(addressPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新地址数据结束====");
		return true;
	}

	@Override
	public List<AddressPO> select(AddressInfo addressInfo)
	{
		logger.info("====查询地址数据开始====");
		logger.debug("====查询地址数据,入参:AddressInfo=[{}]====", addressInfo);
		List<AddressPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from AddressPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(addressInfo.getAddressID()))
		{
			sqlBuffer.append(" and a.addressID=?");
			parameter.putParameterObject(addressInfo.getAddressID());
		}
		if (StringUtils.isNotNullAndBlank(addressInfo.getAddressCode()))
		{
			sqlBuffer.append(" and a.addressCode=?");
			parameter.putParameterObject(addressInfo.getAddressCode());
		}
		if (StringUtils.isNotNullAndBlank(addressInfo.getAddressName()))
		{
			sqlBuffer.append(" and a.addressName=?");
			parameter.putParameterObject(addressInfo.getAddressName());
		}
		if (StringUtils.isNotNullAndBlank(addressInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(addressInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(addressInfo.getParentAddressCode()))
		{
			sqlBuffer.append(" and a.parentAddressCode=?");
			parameter.putParameterObject(addressInfo.getParentAddressCode());
		}
		if (StringUtils.isNotNullAndBlank(addressInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(addressInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询地址数据,出参:List<AddressPO>=[{}]====", returnList);
		logger.info("====查询地址数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(AddressPO addressPO)
	{
		logger.info("====删除地址数据开始====");
		logger.debug("====删除地址数据,入参:AddressPO=[{}]====", addressPO);
		entityManager.remove(addressPO);
		logger.debug("====删除地址数据,出参:boolean=[{}]====", true);
		logger.info("====删除地址数据结束====");
		return true;
	}

	@Override
	public Long count(AddressPO addressPO)
	{
		logger.info("====统计地址数据开始====");
		logger.debug("====统计地址数据,入参:AddressPO=[{}]====", addressPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from AddressPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(addressPO.getAddressID()))
		{
			sqlBuffer.append(" and a.addressID=?");
			parameter.putParameterObject(addressPO.getAddressID());
		}
		if (StringUtils.isNotNullAndBlank(addressPO.getAddressCode()))
		{
			sqlBuffer.append(" and a.addressCode=?");
			parameter.putParameterObject(addressPO.getAddressCode());
		}
		if (StringUtils.isNotNullAndBlank(addressPO.getAddressName()))
		{
			sqlBuffer.append(" and a.addressName=?");
			parameter.putParameterObject(addressPO.getAddressName());
		}
		if (StringUtils.isNotNullAndBlank(addressPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(addressPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(addressPO.getParentAddressCode()))
		{
			sqlBuffer.append(" and a.parentAddressCode=?");
			parameter.putParameterObject(addressPO.getParentAddressCode());
		}
		if (StringUtils.isNotNullAndBlank(addressPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(addressPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计地址数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计地址数据结束====");
		return returnLong;
	}

}
